-- Copyright (c) 2015 Shunsuke Shimizu (grafi)
-- Copyright (c) 2015,2016 Lucky Byte, Inc.
--
-- https://github.com/grafi-tt/lunajson
--
--[[
The MIT License (MIT)

Copyright (c) 2015 Shunsuke Shimizu (grafi)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]

local inf = math.huge
local byte, char, sub = string.byte, string.char, string.sub
local setmetatable = setmetatable
local floor = math.floor

local _ENV = nil

local hextbl = {
    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, inf, inf, inf, inf, inf, inf,
    inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, inf, inf, inf, inf, inf, inf, inf, inf, inf,
    inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
    inf, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, inf, inf, inf, inf, inf, inf, inf, inf, inf,
}
hextbl.__index = function()
    return inf
end
setmetatable(hextbl, hextbl)

return function(myerror)
    local escapetbl = {
        ['"']  = '"',
        ['\\'] = '\\',
        ['/']  = '/',
        ['b']  = '\b',
        ['f']  = '\f',
        ['n']  = '\n',
        ['r']  = '\r',
        ['t']  = '\t'
    }
    escapetbl.__index = function()
        myerror("invalid escape sequence")
    end
    setmetatable(escapetbl, escapetbl)

    local surrogateprev = 0

    local function subst(ch, rest)
        -- 0.000003814697265625 = 2^-18
        -- 0.000244140625 = 2^-12
        -- 0.015625 = 2^-6
        local u8
        if ch == 'u' then
            local c1, c2, c3, c4 = byte(rest, 1, 4)
            local ucode = hextbl[c1-47] * 0x1000 + hextbl[c2-47] * 0x100 + hextbl[c3-47] * 0x10 + hextbl[c4-47]
            if ucode == inf then
                myerror("invalid unicode charcode")
            end
            rest = sub(rest, 5)
            if ucode < 0x80 then -- 1byte
                u8 = char(ucode)
            elseif ucode < 0x800 then -- 2byte
                u8 = char(0xC0 + floor(ucode * 0.015625), 0x80 + ucode % 0x40)
            elseif ucode < 0xD800 or 0xE000 <= ucode then -- 3byte
                u8 = char(0xE0 + floor(ucode * 0.000244140625), 0x80 + floor(ucode * 0.015625) % 0x40, 0x80 + ucode % 0x40)
            elseif 0xD800 <= ucode and ucode < 0xDC00 then -- surrogate pair 1st
                if surrogateprev == 0 then
                    surrogateprev = ucode
                    if rest == '' then
                        return ''
                    end
                end
            else -- surrogate pair 2nd
                if surrogateprev == 0 then
                    surrogateprev = 1
                else
                    ucode = 0x10000 + (surrogateprev - 0xD800) * 0x400 + (ucode - 0xDC00)
                    surrogateprev = 0
                    u8 = char(0xF0 + floor(ucode * 0.000003814697265625), 0x80 + floor(ucode * 0.000244140625) % 0x40, 0x80 + floor(ucode * 0.015625) % 0x40, 0x80 + ucode % 0x40)
                end
            end
        end
        if surrogateprev ~= 0 then
            myerror("invalid surrogate pair")
        end
        return (u8 or escapetbl[ch]) .. rest
    end

    local function surrogateok()
        return surrogateprev == 0
    end

    return {
        subst = subst,
        surrogateok = surrogateok
    }
end
